package com.itheima.youdianda.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.youdianda.domain.YddUserData;
import com.itheima.youdianda.service.YddUserDataService;
import com.itheima.youdianda.mapper.YddUserDataMapper;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;

/**
 * @author YUAN
 * @description 针对表【ydd_user_data】的数据库操作Service实现
 * @createDate 2022-09-06 18:36:39
 */
@Service
public class YddUserDataServiceImpl extends ServiceImpl<YddUserDataMapper, YddUserData>
        implements YddUserDataService {


    //根据action操作点赞/收藏的添加或者删除
    @Override
    public void updateWithAction(HttpSession session, String action, Integer type, Integer article_id) {
        Integer userId = Integer.parseInt((String) session.getAttribute("userId"));
        YddUserData userData = new YddUserData();
        userData.setUserId(userId);
        userData.setArticleId(article_id);
        userData.setType(type);
        //判断action是add还是del

        if (action.equals("add")) {
            this.save(userData);
        } else {
            //条件构造器
            LambdaQueryWrapper<YddUserData> lqw = new LambdaQueryWrapper<>();
                 lqw.eq(YddUserData::getUserId, userId)
                    .eq(YddUserData::getArticleId, userData.getArticleId())
                    .eq(YddUserData::getType, userData.getType());
                 this.remove(lqw);
        }
    }
}




